package com.timi_licai.timi_licai_account.utils;

import android.util.Log;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

public class KeyLineUtils {

    /**
     * 格式化月份 1月改为01
     *
     * @param str
     * @return
     */
    public static String LeftPad_Tow_Zero(int str) {
        String ret = null;
        java.text.DecimalFormat format = new java.text.DecimalFormat("00");
        ret = format.format(str);
        return ret;
    }

    /**
     * @param mPageNumber
     * @param
     * @return
     */
    public static Calendar getSelectCalendar(int mPageNumber) {
        Calendar calendar = Calendar.getInstance();
        Log.d("!!", "---------------------------------------");
        Log.d("!!", "pageNumber:-->" + mPageNumber);
        Log.d("!!", "current:calendar-->" + DateUtils.parseDate(calendar.getTime()));
        Log.d("&&", "当前页的日期--》" + DateUtils.parseDate(calendar.getTime()));
        if (mPageNumber > 500) {
            for (int i = 0; i < mPageNumber - 500; i++) {
                calendar = setNextWeek(calendar);
                Log.d("&&", "前一页的日期--》" + DateUtils.parseDate(calendar.getTime()));
            }
        } else if (mPageNumber < 500) {
            for (int i = 0; i < 500 - mPageNumber; i++) {
                calendar = setPrevWeek(calendar);
                Log.d("&&", "后一页的日期--》" + DateUtils.parseDate(calendar.getTime()));
            }
        }
        return calendar;
    }

    /**
     * 向前滑动时调用此方法 向前加载一周
     *
     * @param calendar
     * @return
     */
    public static Calendar setPrevWeek(Calendar calendar) {

        int iWeek = calendar.get(Calendar.WEEK_OF_MONTH);
        int iMonthViewCurrentMonth = calendar.get(Calendar.MONTH);
        int iMonthViewCurrentYear = calendar.get(Calendar.YEAR);
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
        if (iWeek == 1) {
            Log.d("++", "iMonthViewCurrentMonth+-->" + iMonthViewCurrentMonth);
            if (iMonthViewCurrentMonth == 1) {
                // iMonthViewCurrentMonth = 11;
                iMonthViewCurrentYear--;
                Log.d("++", "year" + iMonthViewCurrentYear);
            }
            iMonthViewCurrentMonth--;

        }
        iWeek--;
        calendar.add(Calendar.DAY_OF_MONTH, -dayOfWeek - 6);
        calendar.set(Calendar.MONTH, iMonthViewCurrentMonth);//设置下一个月的月份
        calendar.set(Calendar.YEAR, iMonthViewCurrentYear);//设置下一个月份的年
        Log.d("!!", "前一周的日期--》" + DateUtils.parseDate(calendar.getTime()));
        return calendar;
    }

    /**
     * 向后加载时调用此方法 每次向后加载一周
     *
     * @param calendar
     * @return
     */
    private static Calendar setNextWeek(Calendar calendar) {

        int week = calendar.get(Calendar.WEEK_OF_MONTH);//得到目前是本月的第几周

        int totalWeek = calendar.getActualMaximum(Calendar.WEEK_OF_MONTH);//获得一个月有少周
        int iMonthViewCurrentMonth = calendar.get(Calendar.MONTH);
        int iMonthViewCurrentYear = calendar.get(Calendar.YEAR);
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
        if (week == totalWeek) {
            if (iMonthViewCurrentMonth == 11) {
                iMonthViewCurrentYear++;
            }
            iMonthViewCurrentMonth++;
        }
        week++;
        calendar.add(Calendar.DAY_OF_MONTH, 8 - dayOfWeek);
        Date date = calendar.getTime();
        calendar.set(Calendar.MONTH, iMonthViewCurrentMonth);//设置下一个月的月份
        Date month = calendar.getTime();
        calendar.set(Calendar.YEAR, iMonthViewCurrentYear);//设置下一个月份的年
        Date time = calendar.getTime();
        Log.d("!!", "后一周的日期：--》" + DateUtils.parseDate(time));
        return calendar;
    }

    /**
     * 得到日期的集合
     *
     * @param calStartDate
     * @return
     */
    public static List<Date> getDates(Calendar calStartDate) {
        List<Date> alArrayList = new ArrayList<Date>();
        //得到当前的日期
        Calendar instance = Calendar.getInstance();
        int day1 = calStartDate.get(Calendar.DAY_OF_MONTH);
        int day2 = instance.get(Calendar.DAY_OF_MONTH);
        int month1 = calStartDate.get(Calendar.MONTH);
        int month2 = instance.get(Calendar.MONTH);
        int year1 = calStartDate.get(Calendar.YEAR);
        int year2 = instance.get(Calendar.YEAR);
        boolean isCurrent = ((day1 == day2) && (month1 == month2) && (year1 == year2));

        if (isCurrent) {
            Calendar calendar = Calendar.getInstance();
            int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);

            calendar.add(Calendar.DAY_OF_MONTH, -(dayOfWeek - 1));
            Log.d("%%", "改变日期后：" + DateUtils.parseDate(calendar.getTime()));
            for (int i = 0; i < 7; i++) {
                alArrayList.add(calendar.getTime());
                calendar.add(Calendar.DAY_OF_MONTH, 1);
            }
        } else {
            for (int i = 1; i <= 7; i++) {
                alArrayList.add(calStartDate.getTime());
                calStartDate.add(Calendar.DAY_OF_MONTH, 1);
            }
        }
        return alArrayList;
    }

    public static List<String> addDataToList() {
        List<String> ret = new LinkedList<>();
        for (int i = 1; i < 10; i++) {
            ret.add(i + "");
        }
        ret.add("AC");
        ret.add("0");
        ret.add("OK");
        return ret;
    }

}
//http://10.0.160.60:8080/jlx.mp4